package server.config.processor;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;
import com.ita.project1.viewController.View;

import server.command.receive.Command;

public class ConfigProcessor {
	private static HashMap<String, Command> hp;
	private static HashMap<String, View> vhp;
	
	public static ConfigProcessor getInstance(String filename, String vFileName){
		return new ConfigProcessor(filename,vFileName);
	}
	
	public static HashMap<String, Command> getHp() {
		return hp;
	}

	private ConfigProcessor(String filename, String vFileName){
		hp=loadConfigFromProperties(filename);
		vhp=loadViewConfigFromProperties(vFileName);
	}
	
	private HashMap<String, Command> loadConfigFromProperties(String filename) {
		//reflect
		try {
			hp = new HashMap<String, Command>();
			Properties list = new Properties();
			list.load(new FileInputStream("./" + filename));
			Set<String> urlList = list.stringPropertyNames();
			for (String cmd : urlList) {
				Command m = (Command) Class.forName(list.getProperty(cmd)).newInstance();
				hp.put(cmd, m);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hp;
	}
	
	private HashMap<String, View> loadViewConfigFromProperties(String filename) {
		//reflect
		try {
			vhp = new HashMap<String, View>();
			Properties list = new Properties();
			list.load(new FileInputStream("./" + filename));
			Set<String> viewList = list.stringPropertyNames();
			for (String viewName : viewList) {
				View view = (View) Class.forName(list.getProperty(viewName)).newInstance();
				vhp.put(viewName, view);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return vhp;
	}

	public static HashMap<String, View> getVhp() {
		return vhp;
	} 
}
